home *** CD-ROM | disk | FTP | other *** search
/ PD Collection CD 1 / PD Collection CD 1.iso / programer2 / icon / Source / Icont / C / Tlocal < prev    next >
Encoding:
Text File  |  1990-07-20  |  7.3 KB  |  383 lines

  1. /*
  2.  *  tlocal.c -- functions needed for different systems.
  3.  */
  4.  
  5. #include "../h/config.h"
  6.  
  7. /*
  8.  * The following code is operating-system dependent [@tlocal.01].
  9.  *  Routines needed by different systems.
  10.  */
  11.  
  12. #if PORT
  13. /* place to put anything system specific */
  14. Deliberate Syntax Error
  15. #endif                    /* PORT */
  16.  
  17. #if AMIGA
  18. #if AZTEC_C
  19. /*
  20.  * abs
  21.  */
  22. abs(i)
  23. int i;
  24. {
  25.    return ((i<0)? (-i) : i);
  26. }
  27.  
  28. /*
  29.  * getfa - get file attribute -1 == OK, 0 == ERROR, 1 == DIRECTORY
  30.  */
  31. getfa()
  32. {
  33.    return -1;
  34. }
  35. #endif                    /* AZTEC_C */
  36. #endif                    /* AMIGA */
  37.  
  38. #if ARM
  39. #include "kernel.h"
  40.  
  41. int unlink (const char *name)
  42. {
  43.     _kernel_osfile_block blk;
  44.  
  45.     return (_kernel_osfile(6,name,&blk) <= 0);
  46. }
  47.  
  48. #define QUOTE " \"\t"
  49.  
  50. int armquote (char *str, char **ret)
  51. {
  52.     char *p;
  53.     static char buf[255];
  54.  
  55.     if (strpbrk(str,QUOTE) == NULL)
  56.     {
  57.         *ret = str;
  58.         return strlen(str);
  59.     }
  60.  
  61.     p = buf;
  62.  
  63.     while (*str && p < &buf[255])
  64.     {
  65.         if (strchr(QUOTE,*str))
  66.         {
  67.             if (p > &buf[252])
  68.                 return -1;
  69.  
  70.             *p++ = '\\';
  71.             *p++ = *str;
  72.         }
  73.         else
  74.             *p++ = *str;
  75.  
  76.         ++str;
  77.     }
  78.  
  79.     if (p >= &buf[255])
  80.         return -1;
  81.  
  82.     *p = 0;
  83.     *ret = buf;
  84.     return (p - buf);
  85. }
  86.  
  87. /* Takes a filename, with a ".u1" suffix, and swaps it, IN PLACE, to
  88.  * conform to Archimedes conventions (u1 as a directory).
  89.  * Note that this is a very simplified version. It relies on the following
  90.  * facts:
  91.  *
  92.  *    1. In the ucode link directives, files ALWAYS end in .u1
  93.  *    2. The input filename is writeable.
  94.  *    3. Files which include directory parts conform to Archimedes
  95.  *       format (FS:dir.dir.file). Note that Unix formats such as
  96.  *       "/usr/icon/lib/time" are inherently non-portable, and NOT
  97.  *       supported.
  98.  *
  99.  * This function is only called from readglob() in C.Lglob.
  100.  */
  101. char *flipname(char *name)
  102. {
  103.     char *p = name + strlen(name) - 1;
  104.     char *q = p - 3;
  105.  
  106.     /* Copy the leafname to the end */
  107.     while (q >= name && *q != '.' && *q != ':')
  108.         *p-- = *q--;
  109.  
  110.     /* Insert the "U1." before the leafname */
  111.     *p-- = '.';
  112.     *p-- = '1';
  113.     *p-- = 'U';
  114.  
  115.     return name;
  116. }
  117. #endif                    /* ARM */
  118.  
  119. #if ATARI_ST
  120.  
  121. unsigned long _STACK = 10240;   /*   MNEED ALSO, PLEASE */
  122.  
  123. #endif                    /* ATARI_ST */
  124.  
  125. #if HIGHC_386
  126. #endif                    /* HIGHC_386 */
  127.  
  128. #if MACINTOSH
  129. #if MPW
  130. /* Floating Point Conversion Routine Stubs
  131.  
  132.    These routines, called by printf, are only necessary if floating point
  133.    formatting is used.
  134. */
  135.  
  136. char *ecvt(value,count,dec,sign)
  137. double value;
  138. int count,*dec,*sign;
  139. {
  140. /* #pragma unused(value,count,dec,sign) */
  141. return NULL;
  142. }
  143. fcvt() {}
  144.  
  145.  
  146. /* Routine to set file type and creator.
  147. */
  148.  
  149. #include <Files.h>
  150.  
  151. void
  152. setfile(filename,type,creator)
  153. char *filename;
  154. OSType type,creator;
  155.    {
  156.    FInfo info;
  157.  
  158.    if (getfinfo(filename,0,&info) == 0) {
  159.       info.fdType = type;
  160.       info.fdCreator = creator;
  161.       setfinfo(filename,0,&info);
  162.       }
  163.    return;
  164.    }
  165.  
  166.  
  167. /* Routine to quote strings for MPW
  168. */
  169.  
  170. char *
  171. mpwquote(s)
  172. char *s;
  173.    {
  174.    static char quotechar[] =
  175.      " \t\n\r#;&|()6'\"/\\{}`?E[]+*GH(<>3I7";
  176.    static char *endq = quotechar + sizeof(quotechar);
  177.    int quote = 0;
  178.    char c,d,*sp,*qp,*cp,*q;
  179.    char *malloc();
  180.  
  181.    sp = s;
  182.    while (c = *sp++) {
  183.       cp = quotechar;
  184.       while ((d = *cp++) && c != d)
  185.      ;
  186.       if (cp != endq) {
  187.          quote = 1;
  188.      break;
  189.      }
  190.       }
  191.    if (quote) {
  192.       qp = q = malloc(4 * strlen(s) + 1);
  193.       *qp++ = '\'';
  194.       sp = s;
  195.       while (c = *sp++) {
  196.      if (c == '\'') {
  197.         *qp++ = '\'';
  198.         *qp++ = '6';
  199.         *qp++ = '\'';
  200.         *qp++ = '\'';
  201.         quote = 1;
  202.         }
  203.      else *qp++ = c;
  204.      }
  205.       *qp++ = '\'';
  206.       *qp++ = '\0';
  207.       }
  208.    else {
  209.       q = malloc(strlen(s) + 1);
  210.       strcpy(q,s);
  211.       }
  212.    return q;
  213.    }
  214.  
  215.  
  216. /*
  217.  * SortOptions -- sorts icont options so that options and file names can
  218.  * appear in any order.
  219.  */
  220. void
  221. SortOptions(argv)
  222. char *argv[];
  223.    {
  224.    char **last,**p,*q,**op,**fp,**optlist,**filelist,opt,*s,*malloc();
  225.    int size,error = 0;;
  226.  
  227.    /*
  228.     * Count parameters before -x.
  229.     */
  230.    ++argv;
  231.    for (last = argv; *last != NULL && strcmp(*last,"-x") != 0; ++last)
  232.       ;
  233.    /*
  234.     * Allocate a work area to build separate lists of options
  235.     * and filenames.
  236.     */
  237.    size = (last - argv + 1) * sizeof(char*);
  238.    optlist = filelist = NULL;
  239.    op = optlist = (char **)malloc(size);
  240.    fp = filelist = (char **)malloc(size);
  241.    if (optlist && filelist) {            /* if allocations ok */
  242.       for (p = argv; (s = *p); ++p) {        /* loop thru args */
  243.          if (error) break;
  244.      if (s[0] == '-' && (opt = s[1]) != '\0') { /* if an option */
  245.         if (q = strchr(Options,opt)) {    /* if valid option */
  246.            *op++ = s;
  247.            if (q[1] == ':') {        /* if has a value */
  248.           if (s[2] != '\0') s += 2;    /* if value in this word */
  249.           else s = *op++ = *++p;    /* else value in next word */
  250.           if (s) {            /* if next word exists */
  251.              if (opt == 'S') {        /* if S option */
  252.             if (s[0] == 'h') ++s;    /* bump past h (??) */
  253.             if (s[0]) ++s;        /* bump past letter */
  254.             else error = 3;        /* error -- no letter */
  255.             if (s[0] == '\0') {    /* if value in next word */
  256.                if ((*op++ = *++p) == NULL)
  257.                      error = 4;    /* error -- no next word */
  258.                }
  259.             }
  260.              }
  261.           else error = 1;    /* error -- missing value */
  262.           }
  263.            }
  264.            else error = 2;        /* error -- invalid option */
  265.         }
  266.      else {                    /* else a file */
  267.         *fp++ = s;
  268.         }
  269.      }
  270.       *op = NULL;
  271.       *fp = NULL;
  272.       if (!error) {
  273.      p = argv;
  274.      for (op = optlist; *op; ++op) *p++ = *op;
  275.      for (fp = filelist; *fp; ++fp) *p++ = *fp;
  276.      }
  277.       }
  278.    if (optlist) free(optlist);
  279.    if (filelist) free(filelist);
  280.    return;
  281.    }
  282. #endif                    /* MPW */
  283. #endif                    /* MACINTOSH */
  284.  
  285. #if MSDOS
  286.  
  287. #if MICROSOFT
  288.  
  289. pointer xmalloc(n)
  290.    long n;
  291.    {
  292.    return calloc((size_t)n,sizeof(char));
  293.    }
  294. #endif                    /* MICROSOFT */
  295.  
  296. #if MICROSOFT || LATTICE
  297. int _stack = (8 * 1024);
  298. #endif                    /* MICROSOFT || LATTICE */
  299.  
  300. #if TURBO
  301. extern unsigned _stklen = 8192;
  302. #endif                    /* TURBO */
  303. #endif                    /* MSDOS */
  304.  
  305. #if MVS || VM
  306. #if SASC
  307. #include <options.h>
  308. char _linkage = _OPTIMIZE;
  309.  
  310. #if MVS                 /* expect dsnames, not DDnames, as file names */
  311. char *_style = "tso:";
  312. #define SYS_OSVS
  313. #else                    /* MVS */
  314. #define SYS_CMS
  315. #endif                    /* MVS */
  316.  
  317. #define RES_IOUTIL
  318. #define RES_DSNAME
  319. #if VM
  320. #define BIMODAL_CMS
  321. #endif                    /* VM */
  322.  
  323. #include <resident.h>
  324.  
  325. #if VM
  326. #include <cmsexec.h>
  327. #endif                    /* VM */
  328. /*
  329.  * No execvp, so turn it into a call to system.  (Then caller can exit.)
  330.  * In VM, put the ICONX command on the CMS stack, and someone else will
  331.  * do it after we're gone.  (system would clobber the user area.)
  332.  */
  333. int sysexec(cmd, argv)
  334.    char *cmd;
  335.    char **argv;
  336.    {
  337. #if MVS
  338.       char *prefix = "tso:";
  339. #else                    /* MVS */
  340.       char *prefix = "";
  341. #endif                    /* MVS */
  342.       int cmdlen = strlen(cmd) + strlen(prefix) + 1;
  343.       char **p;
  344.       char *cmdstr, *next;
  345.  
  346.       for(p = argv+1; *p; ++p)
  347.          cmdlen += strlen(*p) + 1;
  348.       cmdstr = malloc(cmdlen);      /* blithely ignoring failure...  */
  349.       strcpy(cmdstr, prefix);
  350.       strcat(cmdstr, cmd);
  351.       next = cmdstr + strlen(prefix) + strlen(cmd);
  352.       for (p = argv+1; *p; ++p)
  353.          {
  354.              *next = ' ';
  355.              strcpy(next+1, *p);
  356.              next += strlen(*p) + 1;
  357.           }
  358.       *next = '\0';
  359. #if MVS
  360.       return(system(cmdstr));
  361. #else                    /* MVS */
  362.       cmspush(cmdstr);
  363.       return NormalExit;
  364. #endif                    /* MVS */
  365.    }
  366. #endif                    /* SASC */
  367. #endif                    /* MVS || VM */
  368.  
  369. #if OS2
  370. #endif                    /* OS2 */
  371.  
  372. #if UNIX
  373. #endif                    /* UNIX */
  374.  
  375. #if VMS
  376. #endif                    /* VMS */
  377.  
  378. /*
  379.  * End of operating-system specific code.
  380.  */
  381.  
  382. static char *tjunk;            /* avoid empty module */
  383.